package edu.nf.takeoutdemo.service.impl;

import edu.nf.takeoutdemo.commons.context.BaseContext;
import edu.nf.takeoutdemo.mapper.DishMapper;
import edu.nf.takeoutdemo.mapper.SetmealMapper;
import edu.nf.takeoutdemo.mapper.ShoppingCartMapper;
import edu.nf.takeoutdemo.model.converter.ShoppingCartConverter;
import edu.nf.takeoutdemo.model.dto.ShoppingCartDTO;
import edu.nf.takeoutdemo.model.entity.Dish;
import edu.nf.takeoutdemo.model.entity.Setmeal;
import edu.nf.takeoutdemo.model.entity.ShoppingCart;
import edu.nf.takeoutdemo.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/29
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;
    private final ShoppingCartConverter shoppingCartConverter;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    @Override
    public void saveShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = shoppingCartConverter.toShoppingCart(shoppingCartDTO);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断当前加入购物车的商品是否已经存在了
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        //如果存在了，且需要数量加一
        if(list!=null&&!list.isEmpty()){
            ShoppingCart cart = list.getFirst();
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
        }else{
            //如果不存在，需要插入一条购物车数据
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            //判断本次添加到购物车的是菜品还是菜单
            if(dishId!=null){
                //本次添加到购物车的菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //本次添加到购物车的套餐
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

    @Override
    public void cleanShoppingCart() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    // todo
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = shoppingCartConverter.toShoppingCart(shoppingCartDTO);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if(list!=null&&!list.isEmpty()){
            shoppingCart = list.getFirst();
            Integer number = shoppingCart.getNumber();
            if(number==1){
                shoppingCartMapper.delete(shoppingCart.getId());
            }else{
                shoppingCart.setNumber(shoppingCart.getNumber() -  1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }
}
